package org.drawlineapi.tools {
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class DashedLine extends IndicativeLine {
		
		public function DashedLine(from:Point, to:Point) {
			super(from, to);			
		}
		
		override public function draw(from:Point, to:Point):void {
			var dist:Number = Point.distance (from, to);
			var dashSize:Number = this.format.dashSize;
			var dashs:int = Math.round (dist / dashSize) as int;
			
			var f:LineFormat;
			
			var rad:Number = Math.atan2 (to.y - from.y, to.x - from.x);
			
			var newFrom:Point = from;
			var newTo:Point = to;
			
			for (var i:int = 0; i < dashs; i ++) {
								
				if (i % 2 == 0 || i == 0) {
					f = this.format;
					f.alpha = 1;
					this.format = f;
				} else {
					f = this.format;
					f.alpha = 0;;
					this.format = f;
				}
				
				this.graphics.moveTo (newFrom.x, newFrom.y);
				
				var radx:Number = Math.cos (rad) * (((i * dashSize) + dashSize));
				var rady:Number = Math.sin (rad) * (((i * dashSize) + dashSize));
				
				newTo = new Point (radx + from.x, rady + from.y);
				
				this.graphics.lineTo (newTo.x, newTo.y);
				
				newFrom = newTo;
							
			}
				
			
		}
	}

}